<?php
if(!empty($arbol)) {
	foreach($arbol as $k=>$v){
		//d($v['DetalleDato']);
		if($v['DetalleDato']['identificador'] == "R") {
			$r = $v['DetalleDato']['valor'];
		}
		elseif($v['DetalleDato']['identificador'] == "S") {
			$s = $v['DetalleDato']['valor'];
		}
		elseif($v['DetalleDato']['identificador'] == "U") {
			$u = $v['DetalleDato']['valor'];
		}
		elseif($v['DetalleDato']['identificador'] == "W") {
			$w = $v['DetalleDato']['valor'];
		}
		elseif($v['DetalleDato']['identificador'] == "V") {
			$hora = $v['DetalleDato']['valor'];
		}
		elseif($v['DetalleDato']['identificador'] == "T") {
			
			$t = $v['DetalleDato']['valor'];
			if(!isset($tmp[$r][$s][$u])) {
				$tmp[$r][$s][$u] = array($v['DetalleDato']['dato_id'] . "**" . $t . " - " . $w . " " . $hora);
			}
			else {
				$tmp[$r][$s][$u] = array_merge($tmp[$r][$s][$u], array($v['DetalleDato']['dato_id'] . "**" . $t . " - " . $w . " " . $hora));
			}
		}
	}


	$url['controller'] = "datos";
	if(isset($mapa) && $mapa) {
		$url['action'] = "mapa";
		$archivo = false;
	}
	else {
		$url['action'] = "archivos";
		$archivo = true;
	}

	$i=-1;
	foreach($tmp as $k=>$v) {
		$padreA = 0;
		$i++;
		$js[] = 'Tree[' . $i . ']  = "' . ($i+1) . '|' . $padreA . '|' . $k . '|javascript:void(0);";';
		$padreB = $i+1;
		foreach($v as $k1=>$v1) {
			$i++;
			$js[] = 'Tree[' . $i . ']  = "' . ($i+1) . '|' . $padreB . '|' . $k1 . '|javascript:void(0);";';
			$padreC = $i+1;
			foreach($v1 as $k2=>$v2) {
				$i++;
				$js[] = 'Tree[' . $i . ']  = "' . ($i+1) . '|' . $padreC . '|' . $k2 . '|javascript:void(0);";';
				$padreD = $i+1;
				foreach($v2 as $k3=>$v3) {
					$i++;
					$t = explode("**", $v3);
					if(isset($mapa) && $mapa) {
						$url['abierto'] = $padreD+1;
					}
					$url['file'] = $t[0];
					$link = router::url($url);
					
					$js[] = 'Tree[' . $i . ']  = "' . ($i+1) . '|' . $padreD . '|' . $t[1] . '|' . $link .'";';
				}
			}
		}
	}

	echo $javascript->codeBlock("var Tree = new Array;" . implode("\n", $js));
	echo "<div class='tree'></div>";
	echo $javascript->codeBlock("createTree(Tree, null, ".$abierto.", '".router::url("/")."img/tree/', '".$archivo."');");
}
?>